package dev.pda.display.presentation.page

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.activityViewModels
import dev.pda.display.R
import dev.pda.display.presentation.common.base.BaseViewModelFragment
import dev.pda.display.presentation.page.model.PageData
import dev.pda.display.presentation.page.model.WidgetData
import dev.pda.display.presentation.page.model.WidgetModel
import kotlinx.android.synthetic.main.fragment_page.*

class PageFragment : BaseViewModelFragment<PageViewModel>() {

    companion object {
        const val EXTRA_WIDGET_DATA = "extra_widget_data"

        fun newInstance(widgetData: WidgetData<PageData<WidgetModel<Any>>>?): PageFragment {
            val pageFragment = PageFragment()
            val bundle = Bundle()
            widgetData?.let {
                bundle.putSerializable(EXTRA_WIDGET_DATA, widgetData)
            }
            pageFragment.arguments = bundle
            return pageFragment
        }
    }

    override fun buildViewModel(): PageViewModel {
        val viewModel by activityViewModels<PageViewModel>()
        return viewModel
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_page, container, false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

            it.getSerializable(EXTRA_WIDGET_DATA)?.let { data ->
                viewModel.widgetData = data as WidgetData<PageData<WidgetModel<Any>>>?
            }
        }
    }

    override fun initObserve() {
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        bindView()
    }

    @SuppressLint("ResourceType")
    private fun bindView() {
        viewModel.widgetData?.let { widgetData ->
            pageView.layoutParams = LinearLayout.LayoutParams(
                viewModel.getProgramWidth(),
                viewModel.getProgramHeight()
            )
            pageView.initViews(widgetData)
        }

        /*viewModel.widgetData?.let { widgetData ->
            clPageContainer.layoutParams = RelativeLayout.LayoutParams(
                viewModel.getProgramWidth(),
                viewModel.getProgramHeight()
            )
            val pageVM = widgetData.items?.get(0)
            val bgImageView = ImageView(context)
            bgImageView.apply {
                id = 10000
                layoutParams = ViewGroup.LayoutParams(
                    viewModel.getProgramWidth(),
                    viewModel.getProgramHeight()
                )
                scaleType = ImageView.ScaleType.FIT_XY
            }
            val bgImageUrl = "http" + pageVM?.bg
            Glide.with(requireContext()).load(bgImageUrl).into(bgImageView)
            clPageContainer.addView(bgImageView)
            pageVM?.widgets?.forEachIndexed { index, widget ->
                when (widget) {
                    is ImageWidget -> {
                        //还需要将page上设置page的宽和高
                        //widgetData.items?.get(0)
                        val imageView = ImageView(context)
                        imageView.apply {
                            id = index
                            layoutParams = ViewGroup.LayoutParams(
                                viewModel.getViewWidth(widget.width),
                                viewModel.getViewHeight(widget.height)
                            )
                            scaleType = ImageView.ScaleType.FIT_XY
                        }
                        val imageUrl = "http" + widget.item?.imgUrl
                        Glide.with(requireContext()).load(imageUrl).into(imageView)
                        clPageContainer.addView(imageView)
                    }
                    is TextWidget -> {
                        val textView = TextView(context)
                        textView.apply {
                            id = index
                            text = widget.item?.content
                            setSingleLine(true)
                            textSize = TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_PX,
                                widget.item?.fontSize?.toFloat() ?: 16f,
                                requireContext().resources.displayMetrics
                            )
                            setTextColor(Color.parseColor(widget.item?.fontColor))
                            layoutParams = ViewGroup.LayoutParams(
                                viewModel.getViewWidth(widget.width),
                                viewModel.getViewHeight(widget.height)
                            )
                        }
                        clPageContainer.addView(textView)
                    }
                    is VideoWidget -> {
                        val videoView = VideoView(context)
                        val testPath = File(
                            QueueController().getParentFile(requireContext()),
                            Constants.FILE_DOWNLOAD_PATH + "/" + widget.item?.fileName
                        ).path
                        videoView.apply {
                            id = index
                            initVideoView(testPath)
                            layoutParams = ViewGroup.LayoutParams(
                                viewModel.getViewWidth(widget.width),
                                viewModel.getViewHeight(widget.height)
                            )
                        }
                        clPageContainer.addView(videoView)
                    }
                    is UrlWidget -> {
                        val urlView = UrlView(context)
                        urlView.apply {
                            id = index
                            widget.item?.content?.let { loadUrl(it) }
                            layoutParams = ViewGroup.LayoutParams(
                                viewModel.getViewWidth(widget.width),
                                viewModel.getViewHeight(widget.height)
                            )
                        }
                        clPageContainer.addView(urlView)
                    }
                }

                ConstraintSet().apply {
                    clone(clPageContainer)
                    connect(index, ConstraintSet.TOP, clPageContainer.id, ConstraintSet.TOP)
                    connect(
                        index,
                        ConstraintSet.BOTTOM,
                        clPageContainer.id,
                        ConstraintSet.BOTTOM
                    )
                    connect(index, ConstraintSet.END, clPageContainer.id, ConstraintSet.END)
                    connect(index, ConstraintSet.START, clPageContainer.id, ConstraintSet.START)
                    val x = widget.x?.times(1)?.toFloat() ?: 0f
                    val y = widget.y?.times(1)?.toFloat() ?: 0f
                    setVerticalBias(index, y)
                    setHorizontalBias(index, x)
                    applyTo(clPageContainer)
                }

            }

        }*/

    }

}